//jQuery闭包，匿名方法调用
(function($) {  
	
	//验证合法手机号
	$.fn.isMobile = function(mobile) {
		//支持手机号码，3-4位区号，7-8位直播号码，1-4位分机号
		var reg = /((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
		return reg.test($.trim(mobile)); 
	}
	
	//验证是否是合法邮箱
	$.fn.isMail = function(mail) {
		var reg = /^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/;
		return reg.test($.trim(mail));
	}
	
	//验证字符是否符合密码格式（由数字和字母组成，不能是纯数字或者纯字母，长度为6至20位）
	$.fn.isPwd = function(pwd) {     
		var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
		return reg.test($.trim(pwd));
	};
	
	//验证是否是合法的实名认证身份证号
	$.fn.isIdCard = function(idCard) {
		var reg = /^(([0-9]{14}[xX0-9]{1})|([0-9]{17}[xX0-9]{1}))$/;
		return reg.test($.trim(idCard));
	}
	
	//验证金额（两位小数）
	$.fn.isMoney = function(money) {
		var reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/;
		return reg.test($.trim(money));
	}

	//验证字符是否为QQ号
	$.fn.isQQ = function(QQ) {
		var reg = /^[1-9][0-9]{4,9}$/;
		return reg.test($.trim(QQ));
	}

	//验证是否匹配url地址
	$.fn.isUrl = function(url) {
		//var reg = /HTTP|HTTPS|http|https?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
		//var reg = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
		var reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
		return reg.test($.trim(url));
	}
	
	//验证是否匹配restful形式的URL
	$.fn.isRestfulUrl = function(restfulUrl) {
		var reg = /^(\/[^\/]+)+$/;
		return reg.test($.trim(restfulUrl));
	}
	
	//验证是否是合法的支付宝账户（验证手机号或者邮箱）
	$.fn.isAlipayAccount = function(alipayAccount) {
		var reg = /^1[3|4|5|7|8][0-9]{9}|(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
		return reg.test($.trim(alipayAccount));
	}

	//验证是否是合法的微信账户（仅仅只验证手机号）
	$.fn.isWeChatAccount = function(weChatAccount) {
		var reg = /^1[3|4|5|7|8][0-9]{9}$/;
		return reg.test($.trim(weChatAccount));
	}
	
	//验证是否是正整数，提示：/^[0-9]*[1-9][0-9]*$/这个正则表达式可以0打头，其绝对值为正整数
	$.fn.isPositiveInteger = function(positiveInteger) {
		var reg = /^[1-9][0-9]*$/;
		return reg.test($.trim(positiveInteger));
	}
	
	//JS地图经纬度正则表达式校验
	//校验经度（经度整数部分为0-180,小数部分为0到6位!）
	$.fn.isLongitude = function(longitude) {
		var reg = /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,6})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,6}|180)$/;
		return reg.test($.trim(longitude));
	}
	
	//校验纬度（纬度整数部分为0-90,小数部分为0到6位!）
	$.fn.isLatitude = function(latitude) {
		var reg = /^(\-|\+)?([0-8]?\d{1}\.\d{0,6}|90\.0{0,6}|[0-8]?\d{1}|90)$/;
		return reg.test($.trim(latitude));
	}
	
	//验证联行号（12位数字，首位不能为0）
	$.fn.isAlignmentNumber = function(alignmentNumber) {
		var reg = /^([1-9])(\d{11})$/;
		return reg.test($.trim(alignmentNumber));
	}
	
	//非空验证（为空返回true）
	$.fn.isEmpty = function(obj) {
		if (obj == null || obj == undefined || obj == NaN || obj == {} || obj == [] || $.trim(obj) == '') {
			return true;
		} else {
			return false;
		}
	}
	
	//非空验证（不为空返回true）
	$.fn.isNotEmpty = function(obj) {
		if (obj != null && obj != undefined && obj != NaN && obj != {} && obj != [] && $.trim(obj) != '') {
			return true;
		} else {
			return false;
		}
	}
	
	//比较两个整数的大小（前者大返回true，后者大返回false）
	$.fn.compareInt = function (str1, str2) {
		var int1 = parseInt(str1);
		var int2 = parseInt(str2);
		if (int1 > int2) {
			return true;
		} else {
			return false;
		}
	}
	
	//比较两个浮点数的大小（前者大返回true，后者大返回false）
	$.fn.compareFloat = function (str1, str2) {
		var float1 = parseFloat(str1);
		var float2 = parseFloat(str2);
		if (float1 > float2) {
			return true;
		} else {
			return false;
		}
	}
	
	//验证是否是合法的银行卡号
	$.fn.isBankNo = function(bankNo) {
		var lastNum = bankNo.substr(bankNo.length - 1, 1);
        var first15Num = bankNo.substr(0, bankNo.length - 1);
        var newArr = new Array();
        for (var i = first15Num.length - 1; i > -1; i--) {
            newArr.push(first15Num.substr(i, 1));
        }
        var arrJiShu = new Array();
        var arrJiShu2 = new Array();
        var arrOuShu = new Array();
        for (var j = 0; j < newArr.length; j++) {
            if(( j + 1) % 2 == 1) {
                if (parseInt(newArr[j]) * 2 < 9) {
                	arrJiShu.push(parseInt(newArr[j]) * 2);
                } else {
                	arrJiShu2.push(parseInt(newArr[j]) * 2);
                }
            } else {
            	arrOuShu.push(newArr[j]);
            }
        }
        var jishu_child1 = new Array();
        var jishu_child2 = new Array();
        for (var h = 0; h < arrJiShu2.length; h++) {
            jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
            jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
        }
        var sumJiShu = 0;
        var sumOuShu = 0;
        var sumJiShuChild1 = 0;
        var sumJiShuChild2 = 0;
        var sumTotal = 0;
        for (var m = 0; m < arrJiShu.length; m++) {
            sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
        }
        for (var n = 0; n < arrOuShu.length; n++) {
            sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
        }
        for (var p = 0; p < jishu_child1.length; p++) {
            sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
            sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
        }
        sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
        var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
        var luhm = 10 - k;
        if (lastNum == luhm) {
        	return true;
        } else {
        	return false;
        }
	}
	
	//验证是否是合法的身份证号（15位和18位）
	$.fn.isIdCardNo = function(idCardNo) {
        var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
        if (regIdCard.test(idCardNo)) {
        	if (idCardNo.length == 18) {
                var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
                var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2);
                var idCardWiSum = 0;
                for (var i = 0; i < 17; i++) {
                    idCardWiSum += idCardNo.substring(i, i+1) * idCardWi[i];
                }
                var idCardMod = idCardWiSum % 11;
                var idCardLast = idCardNo.substring(17);
                if (idCardMod == 2) {
                    if (idCardLast == "X" || idCardLast == "x") {
                    	return true;
                    } else {
                    	return false;
                    }
               } else {
                    if (idCardLast == idCardY[idCardMod]) {
                    	return true;
                    } else {
                        return false;
                    }
                }
            }
        } else {
        	return false;
        }
	}
	
	//验证统一社会信用代码：例如：91350100M000100Y43、91430382092581024J
	$.fn.isCreditCode = function(creditCode) {
		if (creditCode == null) {
			return false;
		}
		if (creditCode.length != 18) {
			return false;
		}
		var regex = /^([0-9ABCDEFGHJKLMNPQRTUWXY]{2})([0-9]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{10})$/;
		if (!regex.test(creditCode)) { 
			return false;
		}
		var credit_contain_chars = '0123456789ABCDEFGHJKLMNPQRTUWXY';
		var credit_weight = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
		var sum = 0;
		for (var i = 0; i < creditCode.length - 1; i++) {
			var c = creditCode.charAt(i);
			sum += credit_contain_chars.indexOf(c) * credit_weight[i];
		}
		var c18 = (31 - sum % 31) == 31 ? 0 : 31 - sum % 31;
		if (credit_contain_chars.charAt(c18) == creditCode.charAt(17)) {
			return true;
		}
		return false;
	}
	
	//验证组织机构代码：例如：59467239-9、7765XXXX-2
	$.fn.isOrganCode = function(organCode) {
		if (organCode == null) {
			return false;
		}
		if (organCode.length != 10 && organCode.length != 9) {
			return false;
		}
		if (organCode.length == 10 && organCode.charAt(8) != '-') {
			return false;
		}
		if (organCode.length == 10 && organCode.charAt(8) == '-') {
			organCode = organCode.substring(0, 8) + organCode.substring(9);
		}
		var regex = /^([0-9ABCDEFGHJKLMNPQRTUWXY]{8})([0-9X])$/;
		if (!regex.test(organCode)) { 
			return false;
		}
	    var organ_contain_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    var organ_weight = [3, 7, 9, 10, 5, 8, 4, 2];
		var sum = 0;
		for (var i = 0; i < organCode.length - 1; i++) {
			var c = organCode.charAt(i);
			sum += organ_contain_chars.indexOf(c) * organ_weight[i];
		}
		var c9 =  (11 - sum % 11) == 11 ? 0 : 11 - sum % 11;
		var checkCode = c9 == 10 ? "X" : new Number(c9).toString();
		if (checkCode === organCode.substring(8)) {
			return true;
		}
		return false;
	}
	
})(jQuery); 
